package io.scnb.design.util;

import java.util.*;

public class TopicRandomUtil {
    /**
     * 乱序map元素，记录对应答案的key
     * @param option 原始的选项
     * @param key    原始的答案序号
     * @return
     */
    static public Topic random(Map<String, String> option, String key) {
        // 取出选项的序号
        Set<String> keySet = option.keySet();
        ArrayList<String> keyList = new ArrayList<String>(keySet);
        // 将选项序号乱序
        Collections.shuffle(keyList);
        HashMap<String, String> optionNew = new HashMap<String, String>();
        int idx = 0;
        String keyNew = "";
        // 遍历原来的选项序号(A,B,C,D)
        for (String next : keySet) {
            // 从乱序后的问题列表中取出问题
            String randomKey = keyList.get(idx++);
            // 如果当前遍历到的next序号等于原来的答案序号，则新的答案序号为当前的randomKey
            if (key.equals(next)) {
                keyNew = randomKey;
            }
            optionNew.put(randomKey, option.get(next));
        }
        return new Topic(optionNew, keyNew);
    }
}
